export default class Util {
    public static changeGroup(node: cc.Node, group: string) {
        node.group = group;
        let collider: cc.PhysicsCollider = node.getComponent(cc.PhysicsCollider);
        if (collider != null) {
            collider.apply();
        }
        node.children.forEach(function (childNode: cc.Node) {
            Util.changeGroup(childNode, group);
        });
    }


    public static localConvertWorldPoint(node: cc.Node): cc.Vec2 {
        if (node) {
            return node.convertToWorldSpaceAR(cc.v2(0, 0));
        }
        return null;
    }

    public static getRandomInt(min: number, max: number): number {
        var Range: number = max - min;
        var Rand: number = Math.random();
        return (min + Math.round(Rand * Range));
    }


    public static angle(start: cc.Vec2, end: cc.Vec2) {
        var diff_x = end.x - start.x,
            diff_y = end.y - start.y;
        return 360 * Math.atan(diff_y / diff_x) / (2 * Math.PI);
    }

    public static absAngle(start: cc.Vec2, end: cc.Vec2){
        return Math.abs(this.angle(start, end));
    }

    public static rotateAngle(start: cc.Vec2, end: cc.Vec2): number{
        let angle: number = this.absAngle(start, end);
        if(end.x >= start.x && end.y >= start.y){
            return angle - 90;
        }
        else if(end.x <= start.x && end.y >= start.y){
            return 90 - angle;
        }
        else if(end.x >= start.x && end.y <= start.y){
            return -90 - angle;
        }
        else if(end.x <= start.x && end.y <= start.y){
            return angle - 270;
        }
        return 0;
    }
}